package com.ai.web.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ai.web.service.WxRoleMenuService;
import com.ai.web.vo.WxRoleMenuV;
import com.ai.web.vo.ResultModel;
import com.tiny.persistence.domain.WxRoleMenu;
import com.tiny.persistence.repository.WxRoleMenuRepo;

@Service
@Transactional
public class WxRoleMenuServiceImpl extends BaseServiceImpl<WxRoleMenu, Integer, WxRoleMenuV> implements WxRoleMenuService  {

    @Autowired
    private WxRoleMenuRepo wxRoleMenuRepo;
    
    @Autowired
    public WxRoleMenuServiceImpl(WxRoleMenuRepo wxRoleMenuRepo) {
        super(wxRoleMenuRepo);
    }

    public WxRoleMenuV getWxRoleMenu(int userId) throws Exception {
        return getT(userId);
    }

    public ResultModel saveWxRoleMenu(WxRoleMenuV wxRoleMenuV) throws Exception {
        
        WxRoleMenuV saveV = saveT(wxRoleMenuV);
        
        ResultModel rm = new ResultModel();
        rm.setData(saveV);
        rm.setSuccessCode();
        
        return rm;
    }

    public ResultModel deleteWxRoleMenus(List<Integer> userIds) throws Exception {
        deleteTs(userIds);
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("删除成功！");
        
        return rm;
    }

    public ResultModel updateWxRoleMenus(int roleId, String wxRoleMenuIds) throws Exception {
        
        //WxroleMenuRepo.deleteByGroupId(roleId);

        String[] menuIds = wxRoleMenuIds.split("@");
        
        for(String menuId : menuIds){
            WxRoleMenuV WxroleMenuV = new WxRoleMenuV();
            WxroleMenuV.setWxRoleId(roleId);
            WxroleMenuV.setWxMenuId(Integer.valueOf(menuId));
            saveT(WxroleMenuV);
        }
        
        ResultModel rm = new ResultModel();
        rm.setSuccessCode();
        rm.setMsg("更新成功！");
        
        
        return rm;
    }

	public int deleteByGroupId(int roleId) {
		return wxRoleMenuRepo.deleteByRoleId(roleId);
	}

	public List<WxRoleMenu> findSelectedMenuList(int roleId) {
		return wxRoleMenuRepo.findSelectedMenuList(roleId);
	}

	public ResultModel asignMenu(int roleId, String menuIds) {
		wxRoleMenuRepo.deleteByRoleId(roleId);
		String[] menuIdAttr = menuIds.split("@");
		for (String id : menuIdAttr) {
			WxRoleMenu roleMenu = new WxRoleMenu();
			roleMenu.setWxMenuId(Integer.parseInt(id));
			roleMenu.setWxRoleId(roleId);
			wxRoleMenuRepo.save(roleMenu);
		}
		ResultModel rm = new ResultModel();
		rm.setSuccessCode();
		rm.setMsg("分配成功！");
		return rm;
	}
}
